<?php
namespace Home\Controller;
use Think\Controller;
class AdminController extends PublicController {
    public function index(){
        $info = array(
            '操作系统'=>PHP_OS,
            '运行环境'=>$_SERVER["SERVER_SOFTWARE"],
            'PHP运行方式'=>php_sapi_name(),
            'ThinkPHP版本'=>THINK_VERSION.' [ <a href="http://thinkphp.cn" target="_blank">查看最新版本</a> ]',
            '上传附件限制'=>ini_get('upload_max_filesize'),
            '执行时间限制'=>ini_get('max_execution_time').'秒',
            '服务器时间'=>date("Y年n月j日 H:i:s"),
            '北京时间'=>gmdate("Y年n月j日 H:i:s",time()+8*3600),
            '服务器域名/IP'=>$_SERVER['SERVER_NAME'].' [ '.gethostbyname($_SERVER['SERVER_NAME']).' ]',
            '剩余空间'=>round((disk_free_space(".")/(1024*1024)),2).'M',
            'register_globals'=>get_cfg_var("register_globals")=="1" ? "ON" : "OFF",
            'magic_quotes_gpc'=>(1===get_magic_quotes_gpc())?'YES':'NO',
            'magic_quotes_runtime'=>(1===get_magic_quotes_runtime())?'YES':'NO',
        );
        $catid=M('Catid')->count();
        $ip=M('Ip')->count();
        $zhanghao=M('Zhanghao')->count();
        $this->assign('catid',$catid);
        $this->assign('ip',$ip);
        $this->assign('zhanghao',$zhanghao);
        $this->assign('info',$info);
        $this->display();
    }
//    网站设置
    public function shezhi(){
        $pz=M('Shezhi')->where('id=1')->find();
        $this->assign('pz',$pz);
        $this->display();
    }
    public function doshezhi(){
        $pz=M('Shezhi')->where('id=1')->save($_POST);
        if($pz){
            $this->success('修改成功！');
        }
    }
//    添加管理账号
	public function glzh(){
		$gl=M('Gl')->select();
		if($gl){
			$this->assign('glzh',$gl);
		}
		$this->display();
	}
	public function doglzh(){
		if(empty($_POST['glzh']) or empty($_POST['password'])){
			$this->error('内容不能为空！');
		}
		$gl=M('Gl');
		$zh['name']=$_POST['glzh'];
		$zh['password']=md5($_POST['password']);
		$zh['time']=time();
		$add=$gl->add($zh);
		if($add){
			$this->success('添加成功！');
		}
	}
//	删除管理账号
	public function delgl(){
		$uid=$_GET['uid'];
		if($uid==1){
			$this->error('默认管理员账号不能删除！');
		}
		$gl=M('Gl')->where("id=$uid")->delete();
		if($gl){
			$this->success('删除成功！');
		}
	}
//    领取码
    public function lqm(){
        $lqm=M('Lingquma')->where('id=1')->find();
        $this->assign('lqm',$lqm);
        $this->display();
    }
    public function dolqm(){
        if(empty($_POST['lqm'])){
            $this->error('不能为空！');
        }
        $data['lingquma']=$_POST['lqm'];
        $data['time']=time();
        $lqm=M('Lingquma')->where('id=1')->save($data);
        if($lqm){
            $this->success('修改成功！');
        }
    }
//    分类
    public function fenlei(){
        $fenlei=M('Catid');
        $count = $fenlei->count();
        $page = new \Think\Page($count,10);
        //$page=new Page($count,8);//每页显示条数
        $page->setConfig('first', '首页');
        $page->setConfig('prev', '上一页');
        $page->setConfig('current', '当前');
        $page->setConfig('next', '下一页');
        $page->setConfig('end', '末页');
        $page->setConfig('theme', ' %FIRST% %UP_PAGE% 第 %NOW_PAGE%页 %DOWN_PAGE% %END%');
        $page->rollPage = 3;
        $show = $page->show();
        $list = $fenlei
            ->limit($page->firstRow, $page->listRows)
            ->order('id desc')
            ->select();
        $this->assign('fl',$list);
        $this->display();
    }
//    添加分类
    public function addfenlei(){
        $this->display();
    }
    public function dofenlei(){
        if(empty($_POST['mc'])){
            $this->error('名称不能为空！');
        }
        $fenlei['mc']=$_POST['mc'];
        $fenlei['time']=time();
        $fl=M('Catid')->add($fenlei);
        if($fl){
            $this->success('添加成功！');
        }
    }
	//修I改分类
	public function gaicatid(){
		$tid=$_GET['catid'];
		$catid=M('Catid')->where("id=$tid")->find();
		$this->assign('catid',$catid);
		$this->display();
	}
	public function dogaicatid(){
		$cid=$_POST['catid'];
		$data['mc']=$_POST['mc'];
		$catid=M('Catid')->where("id=$cid")->save($data);
		if($catid){
			$this->success('修改成功！');
		}
	}
//    删除分类
    public function catiddel(){
        $tid=$_GET['catid'];
        $catid=M('Catid')->where("id=$tid")->delete();
        $this->success('删除成功');
    }
//    添加账号
    public function fabu(){
        $catid=M('Catid')->select();
        $this->assign('catid',$catid);
        $this->display();
    }
    public function dofabu(){
        if(!isset($_POST['submit']))
        {
            $textArr = Array();
            $newArr = Array();
            $text = $_POST['content'];//获取值
            $text = nl2br($text);//将分行符"\r\n"转义成HTML的换行符"<br />"
            $textArr = explode("<br />",$text);//"<br />"作为分隔切成数组
        //除去数组中的空格
            foreach($textArr as $val)
            {
                if(isset($val) AND $val){
                    $b=array_push($newArr,$val);
                }
            }
            $data['catid']=$_POST['catid'];
            $data['time']=time();
            for($i=0;$i<$b;$i++){
                $data['content']=$newArr[$i];
                M('Zhanghao')->add($data);
            }


                $this->success('账号添加成功！');

        }
    }
//    账号列表
    public function zhanghao(){
        $zh=M('Zhanghao');
        $count = $zh->count();
        $page = new \Think\Page($count, 5);
        //$page=new Page($count,8);//每页显示条数
        $page->setConfig('first', '首页');
        $page->setConfig('prev', '上一页');
        $page->setConfig('current', '当前');
        $page->setConfig('next', '下一页');
        $page->setConfig('end', '末页');
        $page->setConfig('theme', ' %FIRST% %UP_PAGE% 第 %NOW_PAGE%页 %DOWN_PAGE% %END%');
        $page->rollPage = 3;
        $show = $page->show();
        $list = $zh
            ->join('z_catid ON z_zhanghao.catid = z_catid.id')
            ->field(
                array(
                    'z_zhanghao.id',
                    'z_zhanghao.content',
                    'z_zhanghao.zt',
                    'z_zhanghao.time',
                    'z_catid.mc',
                )
            )
            ->limit($page->firstRow, $page->listRows)
            ->order('id desc')
            ->select();
        $this->assign('page',$show);
        $this->assign('zh',$list);
        $this->display();
    }
	//修改内容
	public function xiugai(){
		$tid=$_GET['tid'];
		$zh=M('Zhanghao')->where("id=$tid")->find();
		$this->assign('zh',$zh);
		$this->display();
	}
	public function doxiugai(){
		$tid=$_POST['tid'];
		$data['content']=$_POST['content'];
		$zh=M('Zhanghao')->where("id=$tid")->save($data);
		if($zh){
			$this->success('修改成功！');
		}
	}
	//    删除账号
	public function del(){
        if(empty($_POST['id'])){
            echo"<script>alert('必须选择一个产品,才可以删除!');history.back(-1);</script>";
            exit;
        }else{
            /*如果要获取全部数值则使用下面代码*/

            $id= implode(",",$_POST['id']);
            $zh=M('Zhanghao')->delete($id);
            if($zh){
                $this->success('删除成功！');
            }

        }
	}
//    留言
    public function ly(){
        $ly=M('Ly');
        $count = $ly->count();
        $page = new \Think\Page($count, 5);
        //$page=new Page($count,8);//每页显示条数
        $page->setConfig('first', '首页');
        $page->setConfig('prev', '上一页');
        $page->setConfig('current', '当前');
        $page->setConfig('next', '下一页');
        $page->setConfig('end', '末页');
        $page->setConfig('theme', ' %FIRST% %UP_PAGE% 第 %NOW_PAGE%页 %DOWN_PAGE% %END%');
        $page->rollPage = 3;
        $show = $page->show();
        $list = $ly
            ->limit($page->firstRow, $page->listRows)
            ->order('id desc')
            ->select();
        $this->assign('page',$show);
        $this->assign('ly',$list);
        $this->display();
    }
    public function dodu(){
        $where['id']=I('get.id');
        $data['zt']=1;
        $ly=M('Ly')->where($where)->save($data);
        if($ly){
            $this->success('成功');
        }
    }
    public function dodel(){
        $where['id']=I('get.id');
        $ly=M('Ly')->where($where)->delete();
        if($ly){
            $this->success('成功');
        }
    }
//    友情链接
    public function lianjie(){
        $lianjie=M('Lianjie');
        $data=$lianjie->select();
        //dump($data);
        $this->assign('lj',$data);
        $this->display();
    }
    public function shenhe(){
        $id=I('get.id');
        $zt=I('get.zt');
        $lianjie=M('Lianjie');
        $where['id']=$id;
        $data['zt']=$zt;
        $sava=$lianjie->where($where)->save($data);
        if($sava){
            $this->success('修改成功');
        }else{
            $this->error('修改失败');
        }
    }
    public function delectlj(){
        $where['id']=I('get.id');
        $lianjie=M('Lianjie');
        $del=$lianjie->where($where)->delete();
        if($del){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }
//    修改密码
	public function password(){
		$this->display();
	}
	public function dopassword(){
		$data['password']=md5($_POST['password']);
		$mima=M('Gl')->where('id=1')->save($data);
		if($mima){
			$this->success('修改成功！');
		}
	}
//    注销登录
    public function logout(){
        cookie('name',NULL);
        cookie('uid',NULL);
        $this->success('注销成功！',U('Admin/index'));
    }
}